package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.DietTempEntity;

public class DietTempDAO {

	public boolean insert(Connection conn, DietTempEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"insert into diet_template(add_doc_id,name,add_time,status,bf_hh,bf_memo,lc_hh,lc_memo,dn_hh,dn_memo,tips) values(?,?,?,?,?,?,?,?,?,?,?)");
		ps.setInt(1, entity.getAdd_doc_id());
		ps.setString(2, entity.getName());
		ps.setTimestamp(3, entity.getAdd_time());
		ps.setInt(4, entity.getStatus());
		ps.setString(5, entity.getBf_hh());
		ps.setString(6, entity.getBf_memo());
		ps.setString(7, entity.getLc_hh());
		ps.setString(8, entity.getLc_memo());
		ps.setString(9, entity.getDn_hh());
		ps.setString(10, entity.getDn_memo());
		ps.setString(11, entity.getTips());
		b = ps.executeUpdate() > 0 ? true : false;
		return b;
	}

	public boolean update(Connection conn, DietTempEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"update diet_template set add_doc_id=?,name=?,add_time=?,status=?,bf_hh=?,bf_memo=?,lc_hh=?,lc_memo=?,dn_hh=?,dn_memo=?,tips=? where id=?");
		ps.setInt(1, entity.getAdd_doc_id());
		ps.setString(2, entity.getName());
		ps.setTimestamp(3, entity.getAdd_time());
		ps.setInt(4, entity.getStatus());
		ps.setString(5, entity.getBf_hh());
		ps.setString(6, entity.getBf_memo());
		ps.setString(7, entity.getLc_hh());
		ps.setString(8, entity.getLc_memo());
		ps.setString(9, entity.getDn_hh());
		ps.setString(10, entity.getDn_memo());
		ps.setString(11, entity.getTips());
		ps.setInt(12, entity.getId());
		b = ps.executeUpdate() > 0 ? true : false;
		return b;
	}

	public DietTempEntity findById(Connection conn, int id) throws SQLException {
		DietTempEntity entity = null;
		PreparedStatement ps = conn.prepareStatement("select * from diet_template where id =?");
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			entity = fill(rs);
		}
		return entity;
	}

	public List<DietTempEntity> listByDocId(Connection conn, int id) throws SQLException {
		List<DietTempEntity> list = new LinkedList<>();
		PreparedStatement ps = conn.prepareStatement("select * from diet_template where add_doc_id =?");
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DietTempEntity entity = fill(rs);
			list.add(entity);
		}
		return list;
	}

	private DietTempEntity fill(ResultSet rs) throws SQLException {
		DietTempEntity e = new DietTempEntity();
		e.setAdd_doc_id(rs.getInt("add_doc_id"));
		e.setAdd_time(rs.getTimestamp("add_time"));
		e.setBf_hh(rs.getString("bf_hh"));
		e.setBf_memo(rs.getString("bf_memo"));
		e.setDn_hh(rs.getString("dn_hh"));
		e.setDn_memo(rs.getString("dn_memo"));
		e.setId(rs.getInt("id"));
		e.setLc_hh(rs.getString("lc_hh"));
		e.setLc_memo(rs.getString("lc_memo"));
		e.setName(rs.getString("name"));
		e.setStatus(rs.getInt("status"));
		e.setTips(rs.getString("tips"));
		return e;
	}
}
